$(function () {
  var configMap = {
        basePath: 'http://115.28.198.131:82/yeebrowser/api/v1/',
//        basePath: 'http://localhost:85/yeebrowser/api/v1/',
        newsId: getIdFromUrl()
      },
      jqueryMap = {
        $wrap: $(".wapwrap")
      };

  loadNewsDetail();

  function loadNewsDetail() {
    if (!configMap.newsId) {return false;}

    $.ajax({
      url: configMap.basePath + 'news/content',
      data: {"newsId": configMap.newsId},
      success: function (result) {
        if (!result.error && result.paragraphs && result.paragraphs.length > 0) {
          renderNewsDetail(result);
        }
      }
    })
  }

  function renderNewsDetail(data) {
    var tplArr = [], imageUrl, text;

    data.paragraphs.forEach(function (item) {
      if (item.type === 1) {text = item.text;}
      if (item.type === 2) {imageUrl = item.text;}
    });

    tplArr.push('<div class="newsbox">');
    tplArr.push('<div class="newstitle">' + data.title + '</div>');
    tplArr.push('<div class="newssource"><span class="source">' + data.source + '</span><span class="date">' + data.date + '</span></div>');
    tplArr.push('<div class="newsimage"><img src="' + imageUrl + '"></div>');
    tplArr.push('<div class="newstext">' + text + '</div></div>');

    jqueryMap.$wrap.append(tplArr.join(""));
  }

  function getIdFromUrl() {
    if (location.search) {
      return location.search.split(/\?id=(.*)/)[1];
    }
  }
});